from django.http import JsonResponse
import json

from salary_flow.models import Login


# 分配器

def dispatcher(request):
    # 将请求同意放在request的params中
    print(request.body)
    if request.method == 'GET':
        request.params = request.GET
    elif request.method in ['POST', 'PUT', 'DELETE']:
        request.params = json.loads(request.body)
    print(request)
    # 根据不同的action分派给不同的函数进行处理
    action = request.params['action']
    if action == 'list_login':
        return list_login(request)
    elif action == 'add_login':
        return add_login(request)
    elif action == 'modify_login':
        return modify_login(request)
    elif action == 'del_login':
        return del_login(request)
    elif action == 'login':
        return login(request)

    else:
        return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'}, json_dumps_params={'ensure_ascii': False})

def login(request):

    try:
        username = request.params['username']
        password = request.params['password']
        # 根据 id 从数据库中找到相应的员工记录
        person = Login.objects.get(Lname=username, Lpassword=password)
        return JsonResponse({
            'ret': 0,
            'msg': f'欢迎`{username}`，成功登录绩效管理系统',

        }, json_dumps_params={'ensure_ascii': False})
    except Login.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'名 为`{username}`的员工不存在'
        }, json_dumps_params={'ensure_ascii': False})


def list_login(request):
    # 返回一个 QuerySet 对象 ，包含所有的表记录
    qs = Login.objects.values()

    # 将 QuerySet 对象 转化为 list 类型
    # 否则不能 被 转化为 JSON 字符串
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist}, json_dumps_params={'ensure_ascii': False})


def add_login(request):
    print(request)
    info = request.params['data']

    # 从请求消息中 获取要添加部门的信息
    # 并且插入到数据库中
    # 返回值 就是对应插入记录的对象
    record = Login.objects.create(Lname=info['name'],
                                  Lpassword=info['password'],
                                  Loutteroffice=info['outteroffice'],
                                  Linneroffice=info['inneroffice'],
                                  Ldepartment=info['department'],
                                  Lproject=info['project'],
                                  Linnerlevel=info['innerlevel'],
                                  Loutterlevel=info['outterlevel'],
                                  Lstate=info['state'],
                                  Lstatetext=info['statetext'],
                                  )

    return JsonResponse({'ret': 0, 'id': record.id}, json_dumps_params={'ensure_ascii': False})


def modify_login(request):
    # 从请求消息中 获取修改部门的信息
    # 找到该部门，并且进行修改操作

    login_id = request.params['id']
    newdata = request.params['newdata']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        login = Login.objects.get(id=login_id)
    except login.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{login_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii': False})

    if 'name' in newdata:
        login.Lname = newdata['name']
    if 'password' in newdata:
        login.Lpassword = newdata['password']
    if 'outteroffice' in newdata:
        login.Loutteroffice = newdata['outteroffice']
    if 'inneroffice' in newdata:
        login.Linneroffice = newdata['inneroffice']
    if 'department' in newdata:
        login.Ldepartment = newdata['department']
    if 'project' in newdata:
        login.Lproject = newdata['project']
    if 'innerlevel' in newdata:
        login.Linnerlevel = newdata['innerlevel']
    if 'outterlevel' in newdata:
        login.Loutterlevel = newdata['outterlevel']
    if 'state' in newdata:
        login.Lstate = newdata['state']
    if 'statetext' in newdata:
        login.Lstatetext = newdata['statetext']

    # 注意，一定要执行save才能将修改信息保存到数据库
    login.save()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii': False})


def del_login(request):
    login_id = request.params['id']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        login = Login.objects.get(id=login_id)
    except login.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{login_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii': False})

    # delete 方法就将该记录从数据库中删除了
    login.delete()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii': False})
